package com.xy.annotaion.handler.kotlin.spider

import com.xy.annotaion.handler.kotlin.p
import org.jsoup.Jsoup
import java.io.FileOutputStream

fun main() {
    //100MB cache http request data
    val maxBytes = 1024 * 1024 * 100
    val file = FileOutputStream("forum2.mp4")
    var homeUrl = "https://flv5.bn.netease.com"
    val videoPhaseList =
        "https://flv5.bn.netease.com/66f1d5ee40a3fd7db1a7f2d2f07778b7b2500a7878f6902010f8564a30a729ec8a93b59da1f3209a5ed161a53d30f7efdc6cf75aa895c05fc0fd3a93c279c13cb339e074f585e50782ef1772dd233569736631fd58d182100229a55b857547d2cc24525c0f37a5fef6a79d93d346044992cd4f9224162bbbcc6bf096a4c4cf50cbaa8c232acb2a2a8a95cefde299d6506e1f60df3b0f3cb4.m3u8"
    //jsoup 下载文件默认1MB数据，需要设置才能满足要求
    val videoListOriginData =
        Jsoup.connect(videoPhaseList).ignoreContentType(true).maxBodySize(maxBytes).execute().body()

    p(videoListOriginData)
    val originDataList = videoListOriginData.split("\n")
    for (i in originDataList) {
        p(i);
        if (i?.isNotBlank() && !i.startsWith("#")) {
            val videoPhaseData =
                Jsoup.connect(homeUrl + i).ignoreContentType(true).maxBodySize(maxBytes).execute().bodyAsBytes();
            file.write(videoPhaseData);
        }
    }
    file.flush();
    file.close();

}
